import { createRouter, createWebHistory } from 'vue-router'
// import Home from '@/views/Home/Home.vue'
import Login from '@/components/Login.vue'
import Index from '@/views/Index.vue'

const routes = [
  {
    path: '/Login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    name: 'Index',
    component: Index,
    redirect: "/Home",
    // meta: {
    //   keepAlive: true,  //需要缓存
    //   title: '首页',
    //   isShow:['a','b','c']
    // },
    children: [
      {
        path: '/Home',
        name: 'Home',
        title: '首页',
        redirect: "/Home1",
        icon:'HomeFilled',
        meta: {
          keepAlive: true,  //需要缓存
          name: '首页',
          isShow: ['a', 'b', 'c'],
          icon:'location'
        },
        component: () => import('@/views/Home/Home.vue'),
        children: [
          {
            path: '/Home1',
            name: 'Home1',
            icon:'HomeFilled',
            component: () => import('@/views/Home/Home1.vue'),
          },
          {
            path: '/Home2',
            name: 'Home2',
            icon:'HomeFilled',
            component: () => import('@/views/Home/Home2.vue'),
          },
          {
            path: '/Home3',
            name: 'Home3',
            icon:'HomeFilled',
            component: () => import('@/views/Home/Home3.vue'),
          }
        ]
        
      },
      {
        path: '/List',
        name: 'List',
        title: '列表',
        icon:'Menu',
        meta: {
          keepAlive: true,  //需要缓存
          name: '列表',
          isShow: ['a', 'b', 'c'],
          icon:' Menu '
        },
        component: () => import('@/views/List/List.vue'),
        children: [
          {
            path: '/List1',
            name: 'List1',
            icon:'Menu',
            component: () => import('@/views/List/List1.vue'),
          },
          {
            path: '/List2',
            name: 'List2',
            icon:'Menu',
            component: () => import('@/views/List/List2.vue'),
          },
          {
            path: '/List3',
            name: 'List3',
            icon:'Menu',
            component: () => import('@/views/List/List3.vue'),
          }
        ]
      },
      {
        path: '/User',
        name: 'User',
        title: '角色',
        icon:'UserFilled',
        meta: {
          keepAlive: true,  //需要缓存
          name: '角色',
          isShow: ['a', 'b'],
          icon:'UserFilled '
        },
        component: () => import('@/views/User/User.vue'),
        children: [
          {
            path: '/User1',
            name: 'User1',
            icon:'UserFilled',
            component: () => import('@/views/User/User1.vue'),
          },
          {
            path: '/User2',
            name: 'User2',
            icon:'UserFilled',
            component: () => import('@/views/User/User2.vue'),
          },
          {
            path: '/User3',
            name: 'User3',
            icon:'UserFilled',
            component: () => import('@/views/User/User3.vue'),
          }
        ]
      },
      {
        path: '/Introduce',
        name: 'Introduce',
        title: '介绍',
        icon:'Grid',
        meta: {
          keepAlive: true,  //需要缓存
          name: '介绍',
          isShow: ['a', 'b', 'c'],
          icon:'Grid'
        },
        component: () => import('@/views/Introduce/Introduce.vue'),
        children: [
          {
            path: '/Introduce1',
            name: 'Introduce1',
            icon:'Grid',
            component: () => import('@/views/Introduce/Introduce1.vue'),
          },
          {
            path: '/Introduce2',
            name: 'Introduce2',
            icon:'Grid',
            component: () => import('@/views/Introduce/Introduce2.vue'),
          },
          {
            path: '/Introduce3',
            name: 'Introduce3',
            icon:'Grid',
            component: () => import('@/views/Introduce/Introduce3.vue'),
          }
        ]
      }

    ]
    
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})


/**
 * 全局前置路由守卫，每一次路由跳转前都进入这个 beforeEach 函数
 */
router.beforeEach((to, from, next) => {
  if (to.path == '/Login') {
      // 登录或者注册才可以往下进行
      next();
  } else {
      // 获取 token
      const token = localStorage.getItem('TOKEN'); 
      // token 不存在
      if (token === null || token === '') {
          // ElMessage.error('您还没有登录，请先登录');
          next('/Login');
      } else {
          next();
      }
  }
});

export default router
